//
//  TGCDataCenter.m
//  TGC
//
//

#import "TGCDataCenter.h"

static TGCDataCenter *defaultCenter = nil;

@implementation TGCDataCenter


@synthesize userDict = _userDict;


+(TGCDataCenter *)defaultCenter{
    
    if (defaultCenter == nil) {
        
        defaultCenter = [[super allocWithZone:NULL] init];
    }
    
    return defaultCenter;
}

-(id)init{
    
    self = [super init];
    if (self){
        
        _userDict = [NSMutableDictionary dictionary];
        _usedaddressarr = [[NSMutableArray alloc]init];
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

        [_usedaddressarr addObjectsFromArray: [userDefaults objectForKey:@"uploadArr"]];

    }
    
    return self;
}

+(CGSize)getContentSize:(NSString *)content
            WithMaxSize:(CGSize)maxSize
                   Font:(UIFont *)font
          LineBreakMode:(NSLineBreakMode)lineBreakMode{
    
    if ([content respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
        
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineBreakMode = lineBreakMode;
        NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle};
        CGRect boundingRect = [content boundingRectWithSize:maxSize
                                                    options:NSStringDrawingUsesLineFragmentOrigin
                                                 attributes:attributes context:nil];
        
        return CGSizeMake(ceilf(boundingRect.size.width), ceilf(boundingRect.size.height));
    }
    
    return [content sizeWithFont:font constrainedToSize:maxSize lineBreakMode:lineBreakMode];
}


-(void)loginWithUserDict:(NSDictionary *)dict{
    
    _userDict = [NSMutableDictionary dictionaryWithDictionary:dict];
    
//    [self saveUserName:[_userDict objectForKey:@"username"]
//               userPsw:[_userDict objectForKey:@"password"]];
}

-(void)reloadUserDict:(NSDictionary *)dict{
    
    _userDict = [NSMutableDictionary dictionaryWithDictionary:dict];
}

-(void)logout{
    
    _userDict = nil;

    [self clearUserInfo];
}
#pragma mark - 保存头像链接
-(void)saveUserheadurl:(NSString *)url{
    [APPUserDict setObject:url forKey:@"headpicUrl"];
    [APPUserDict synchronize];

}
#pragma mark - 保存用户信息
-(void)saveUserName:(NSString *)username
           password:(NSString *)password
          authority:(NSString *)authority
              isMan:(NSString *)isMan
           telphone:(NSString *)telphone
        displayname:(NSString *)displayname
           usertype:(NSString *)usertype
             userid:(NSString *)userid
          positinal:(NSString *)positinal{
    
    [APPUserDict setObject:username forKey:@"username"];
    [APPUserDict setObject:password forKey:@"password"];
    [APPUserDict setObject:authority forKey:@"authority"];
    [APPUserDict setObject:isMan forKey:@"isMan"];
    [APPUserDict setObject:telphone forKey:@"telphone"];
    [APPUserDict setObject:displayname forKey:@"displayname"];
    [APPUserDict setObject:usertype forKey:@"usertype"];
    [APPUserDict setObject:userid forKey:@"userid"];
    [APPUserDict setObject:positinal forKey:@"positinal"];

    [APPUserDict synchronize];
    _isLogin=YES;
}
#pragma mark - 保存配置信息
-(void)savebookidarr:(NSArray *)bookidarr
           lixingarr:(NSArray *)lixingarr
            wenduarr:(NSArray *)wenduarr
             timearr:(NSArray *)timearr
            speedarr:(NSArray *)speedarr
           bxareaarr:(NSArray *)bxareaarr
           ydareaarr:(NSArray *)ydareaarr{
    [APPUserDict setObject:bookidarr forKey:@"bookidarr"];
    [APPUserDict setObject:lixingarr forKey:@"lixingarr"];
    [APPUserDict setObject:wenduarr forKey:@"wenduarr"];
    [APPUserDict setObject:timearr forKey:@"timearr"];
    [APPUserDict setObject:speedarr forKey:@"speedarr"];
    [APPUserDict setObject:bxareaarr forKey:@"bxareaarr"];
    [APPUserDict setObject:ydareaarr forKey:@"ydareaarr"];

    [APPUserDict synchronize];

}
#pragma mark - 保存网络开关信息
-(void)saveneedwifi:(NSString *)isneed{
    [APPUserDict setObject:isneed forKey:@"needwifi"];
    [APPUserDict synchronize];

}
#pragma mark - 读取用户信息
-(NSDictionary *)readUserInfo{
    
//    NSDictionary *usernamepasswordKVPairs = (NSDictionary *)[CHKeychain load:g_strKey_USERNAME_PASSWORD];
//    return usernamepasswordKVPairs;
    return nil;
}

#pragma mark - 清除用户信息
-(void)clearUserInfo{
    
//    [CHKeychain delete:g_strKey_USERNAME_PASSWORD];
    [APPUserDict setObject:[APPUserDict objectForKey:@"username"] forKey:@"username"];
    [APPUserDict setObject:@"" forKey:@"userPsw"];
    [APPUserDict setObject:[APPUserDict objectForKey:@"area"] forKey:@"area"];
    [APPUserDict setBool:NO forKey:@"isremember"];
    
    [APPUserDict synchronize];
    _isLogin=NO;
}

-(void)saveSearchHistory:(NSString *)searchKey{
    
    NSMutableArray *tempArray = [[NSMutableArray alloc]
                                 initWithArray:[APPUserDict objectForKey:
                                                g_strKey_SearchedHistory]];
    BOOL bIsHave = NO;
    for (NSString *tempStr in tempArray) {
        
        if ([tempStr isEqualToString:searchKey]) {
            
            bIsHave = YES;
            
            break;
        }
    }
    
    if (!bIsHave) {
        
        [tempArray addObject:searchKey];
        
        [APPUserDict setObject:tempArray forKey:g_strKey_SearchedHistory];
        [APPUserDict synchronize];
    }
}

-(NSMutableArray *)getSearchHistory{
    
    return [APPUserDict objectForKey:g_strKey_SearchedHistory];
}

-(void)clearSearchHistory{
    
    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    
    [APPUserDict setObject:tempArray forKey:g_strKey_SearchedHistory];
    [APPUserDict synchronize];
}

-(void)getnownetworktype{
    
}
-(NSString *)getnowtime{
    
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    
    [dateformatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    NSString *  locationString=[dateformatter stringFromDate:[NSDate date]];
    return locationString;
}

+(NSString *)getDateStringByDate:(NSDate *)date
               withDateFormatter:(NSString *)formatterString{
    if (!formatterString) {
        
        formatterString = @"YYYY-MM-dd HH:mm:ss";
    }
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:formatterString];
    
    return [dateFormatter stringFromDate:date];
}

+(NSString *)getDateStringBySecondStr:(NSString *)secondStr
                    withDateFormatter:(NSString *)formatterString{
    
    if (!formatterString) {
        
       formatterString = @"YYYY-MM-dd HH:mm:ss";
    }
    
    if (secondStr && [secondStr length] > 0) {
        
        NSDate *tempDate = [NSDate dateWithTimeIntervalSince1970:[secondStr doubleValue]];
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:formatterString];
        
        return [dateFormatter stringFromDate:tempDate];
    }
    
    return nil;
}

+(NSString *)getSecondStringByDateStr:(NSString *)dateStr{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [dateFormatter dateFromString:dateStr];
    
    return [NSString stringWithFormat:@"%0.0f",[date timeIntervalSince1970]];
}
-(void)removeusedaddressArray:(NSObject *)object{
    if (![_usedaddressarr containsObject:object]) {
        return;
    }
    [_usedaddressarr removeObject:object];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:_usedaddressarr forKey:@"uploadArr"];
    [userDefaults synchronize];
}
-(void)addusedaddressArray:(NSObject *)object{
    [_usedaddressarr addObject:object];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:_usedaddressarr forKey:@"uploadArr"];
    [userDefaults synchronize];
}
-(NSMutableArray*)getusedaddressArray{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    return [userDefaults objectForKey:@"uploadArr"];

}
-(NSMutableArray *)getusedaddressarr:(NSString *)nowmaddress{
    NSMutableArray *arr=[self getusedaddressArray];
    NSMutableArray *savearr=[[NSMutableArray alloc]init];
    for (int i=0; i<arr.count; i++) {
        NSString*addressstr=[arr objectAtIndex:i];
        NSString*newaddressstr=[addressstr substringToIndex:(addressstr.length-2)];
        if ([newaddressstr isEqualToString:nowmaddress]) {
            NSString*savestr=[addressstr substringFromIndex:(addressstr.length-2)];
            [savearr addObject:savestr];
            
        }
    }
    
    
    return savearr;
}
@end
